/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {
  StyleSheet,
  SafeAreaView,
  BackHandler,
  StatusBar,
  Platform,
  Alert,
    View,Text
} from "react-native";
import { WebView } from "react-native-webview";

type Props = {};

let isBack = false;

export default class App extends Component<Props> {
  constructor(props, context) {
    super(props, context);
    this.state = {
      isUrl: '*'
    };
  }

  componentDidMount() {
    if (Platform.OS !== "ios") StatusBar.setBackgroundColor("#a5a5a5");
    BackHandler.addEventListener("hardwareBackPress", this.onBackAndroid);
    /*this.Timer = setTimeout(() => {
      SplashScreen.hide();
      clearInterval(this.Timer);
    }, 1500);*/
    // check for update
    // code
  }

  componentWillUnmount() {
    BackHandler.removeEventListener("hardwareBackPress", this.onBackAndroid);
    //clearInterval(this.Timer);
  }

  _onNavigationStateChange(navState) {
    isBack = navState.canGoBack;
    console.log(navState)
  }

  /**
   * 处理 物理按键（返回）的情况
   * @returns {boolean}
   */
  onBackAndroid = () => {
    if (isBack) {
      this.webview.goBack();
      return true;
    }
    Alert.alert(
        "退出提示",
        "是否退出应用程序？",
        [
          {
            text: "取消",
            onPress: () => {
              return true;
            }
          },
          {
            text: "退出",
            onPress: () => {
              BackHandler.exitApp();
            }
          }
        ],
        { cancelable: false }
    );

    return true;
    /*if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) {
              BackHandler.exitApp();
            }
            lastBackPressed = Date.now();
            ToastAndroid.show("再按一次退出应用程序", ToastAndroid.SHORT);
            return true;*/
  };

  render() {
    return (
        <SafeAreaView style={styles.container}>
          <WebView
              source={{ uri: this.state.isUrl }}
              //ref={w => (this.webview = w)}
              //startInLoadingState={true}
              //originWhitelist={["*"]}
              //style={{ flex: 1 }}
              //mixedContentMode={"always"}
              //javaScriptEnabled={true}
              //domStorageEnabled={true}
              onNavigationStateChange={this._onNavigationStateChange}
              //thirdPartyCookiesEnabled={true}
              //allowUniversalAccessFromFileURLs={true}
              //cacheEnabled={true}
              //dataDetectorTypes={'all'}
              //bounces={false}
          />
        </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#ffffff"
  }
});